package com.woniu.job.controller.admin;

import com.woniu.job.entity.Admin;
import com.woniu.job.entity.vo.ResultVO;
import com.woniu.job.exception.service.*;
import com.woniu.job.service.IAdminService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;

/**
 * @author sunnylausong
 *
 * 2019/12/24
 * 20:07
 */

@Controller
@RequestMapping("/admin")
@SessionAttributes("loginAdmin")
@Api("后台管理员接口")
public class AdminController {

    @Autowired
    IAdminService adminService;
    @Value("${backUrl}")
    String backUrl;

    /**
     * 登录页面
     * @return
     */
    @GetMapping("/login")
    public String login()
    {
        return "admin/login";
    }

    /**
     * 登录
     * @param account
     * @param password
     * @param model
     * @return
     */
    @PostMapping("/login")
    public String login(String account, String password, Model model)
    {
        try {
            Admin admin = adminService.getByNameAndPassword(account, password);
            model.addAttribute("loginAdmin", admin);
//            return "back/index";
            return "redirect:" + backUrl;
        } catch (ServiceException e) {
            if (e instanceof AdminAccountAndPasswordNotMatch) {
                model.addAttribute("msg", "用户名/密码错误！");
            } else if (e instanceof ForbiddenAdminException) {
                model.addAttribute("msg", "禁用账户，请联系管理员！");
            }
            return "admin/login";
        }
    }

    /**
     * 添加用户
     * @param admin
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public ResultVO add(@RequestBody Admin admin)
    {
        adminService.add(admin);
        return ResultVO.SUC.message("添加成功！");
    }

    /**
     * 获取管理员列表
     * @return
     */
    @ResponseBody
    @GetMapping("/list/{p}")
    public ResultVO list(@PathVariable int p)
    {
        p = p < 1 ? 1 : p;
        return new ResultVO(adminService.getList(p));
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @ResponseBody
    @GetMapping("/del/{id}")
    public ResultVO delete(@PathVariable String id)
    {
        adminService.delete(id);
        return ResultVO.SUC.message("删除成功！");
    }

    /**
     * 修改账户状态
     * @param id
     * @param forbidden bool 是否为禁用操作
     * @return
     */
    @ResponseBody
    @GetMapping("/update/{id}/{forbidden}")
    public ResultVO update(@PathVariable String id, @PathVariable Boolean forbidden)
    {
        if (forbidden) {
            adminService.updateStatus(id, IAdminService.STATUS_FORBIDDEN);
        } else {
            adminService.updateStatus(id, IAdminService.STATUS_NORMAL);
        }
        return ResultVO.SUC.message("修改成功！");
    }

    /**
     * 注销登录
     * @param status
     * @return
     */
    @GetMapping("/logout")
    public String logout(SessionStatus status)
    {
        status.setComplete();
        return "admin/login";
    }

    /**
     *  统一异常处理
     * @param e
     * @return
     */
    @ExceptionHandler(ServiceException.class)
    @ResponseBody
    public ResultVO handle(ServiceException e)
    {
        if (e instanceof AdminExistException) {
            return ResultVO.ERROR.message("账户已经存在！");
        } else if (e instanceof ModifySuperAdminException) {
            return ResultVO.ERROR.message("不能修改超级管理员！");
        }
        return ResultVO.ERROR.message("服务器抽风!");
    }


}
